const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   list:[],//
   left_activeNames: 0,//左侧点击的 默认第一个
   activeNames:['0'],
   list_son:[],//返回第几个 点0返回0,初始默认0
   order_list:[],
   pic_no:0,//图号
   p_no:0,//项目号
   pic_name:"0",//工件名称
   status:"0",//项目状态
   num:0,//订单总额
   name:"0",//当前工序名称
   is_start:"0",//当前工序已生产
   invalid_num:"0",//当前工序废品数
   no:"0",//当前工序编号
   sort:"0",//当前工序状态
   waitHandle:"0",//当前工序待处理
   check_num:"0",//当前工序已质检
   valid_num:"0",//当前工序有效生产
   target_num:0,//当前工序需处理
   radioValue:0,//默认选中
   stage:0,//工序阶段
   need_list:[],//还需生产列表
   status_son:0,
   setworknum:"",//填写内容
   order_id:"",//订单条目id
   options:"",//初始化使用
   waitCheckNum:"",//等待之间数量
   showSkipBtn:"",//是否显示跳过工序按钮
   p_id:"",//生产详情id
   res:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options")
    var that = this
    if(options.id!=undefined){
      this.setData({
        p_id:options.id
      })
      let tmplist = {
        id:options.id
      }
      that.getData(tmplist,options)
    }
    if (options.q) {
      const sceneStr = decodeURIComponent(options.q); // 先解码
      console.log(sceneStr,'53')
      var sceneStrTemp = sceneStr.split('id=');
      console.log(sceneStrTemp)
      if(sceneStrTemp[1]==undefined){
        wx.showToast({
          title: "错误的二维码",
          icon:'none'
        })
      }
      var id = sceneStrTemp[1];

      that.getData({id:id},{id:id})

    }
    if (options.scene) {
      const sceneStr = decodeURIComponent(options.scene); // 先解码
      console.log(sceneStr,'69')
      var sceneStrTemp = sceneStr.split('id=');
      console.log(sceneStrTemp)
      if(sceneStrTemp[1]==undefined){
        wx.showToast({
          title: "错误的二维码",
          icon:'none'
        })
      }
      var id = sceneStrTemp[1];

      that.getData({id:id},{id:id})

    }

  },

  getData(list,options){
    var that = this
    console.log(list,"listtttttttttt")
    request.requestGet('order/show_production_info', list, '正在加载',function (res) {
      console.log(res,"resssssssss")
      if (res.status === 200) {
        that.setData({
          list:res.data.processes,
          res:res,
          order_list:res.data.order,
          p_no:res.data.order.p_no,
          pic_no:res.data.order.pic_no,
          num:res.data.order.num,
          pic_name:res.data.order.pic_name,
          status:res.data.project.status,
          left_activeNames:res.data.currentNo - 1,
          showSkipBtn:res.data.showSkipBtn,
          options:options,
          waitCheckNum:res.data.waitCheckNum,
          // radioValue:res.data.
          order_id:res.data.order.id
        })
        console.log(that.data,"PPPPPPPP")
        console.log(that.data.left_activeNames,"---------")
        that.setinfo(that.data.left_activeNames)
        if(that.data.list.length === 0){
          return
        }
        that.setData({
          status_son:that.data.list[that.data.left_activeNames].status,
        })
        console.log(that.data.list,"listttttttttt")
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载失败',
        icon:'none'
      })
    })
  },

  setDataShow(options){
    var that = this
    let tmplist = {
      id:options.id
    }
    that.getData(tmplist,options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onCollapseChange(e) {
    this.setData({
      activeNames: e.detail // 当前展开的项
    });
  },
  onSidebarChange(e){
    this.setData({
      left_activeNames:e.currentTarget.dataset.id
    })
    this.setinfo(this.data.left_activeNames)
  },
  setinfo(num){
    console.log(this.data.list,"//////////////////")
    if(this.data.list.length === 0){
      //没数据
      return
    }
    console.log(num,"//////////////////---------------")
    let tmpinfo = this.data.list[num]
    console.log(tmpinfo,"tmpinfotmpinfotmpinfo")
    this.setData({
          name:tmpinfo.name,//当前工序名称
          is_start:tmpinfo.is_start,//当前工序已生产
          invalid_num:tmpinfo.invalid_num,//当前工序废品数
          no:tmpinfo.no,//当前工序编号
          sort:tmpinfo.sort,//当前工序状态
          waitHandle:tmpinfo.waitHandle,//当前工序待处理
          check_num:tmpinfo.check_num,//当前工序已质检
          valid_num:tmpinfo.valid_num,//当前工序有效生产
          target_num:tmpinfo.target_num,//当前工序需处理
          need_list:tmpinfo.task,
          status_son:this.data.list[this.data.left_activeNames].status,
          stage:this.data.list[this.data.left_activeNames].stage
    })
    this.setData({
    })
   

    for(let i = 0;i < this.data.need_list.length;i++){
      if(this.data.need_list[i].is_final === 0){
        this.setData({
          radioValue:this.data.need_list[i].id,
        })
        return
      }
    }
  },
  onRadioChange(e){
    if(this.data.radioValue === e.currentTarget.dataset.index){
      return
    }
    this.setData({ radioValue: e.currentTarget.dataset.index });
  },
  submit(e){
    let getlist = []
    var that = this
    for (let i = 0;i<this.data.need_list.length;i++){
      if(this.data.need_list[i].id === this.data.radioValue){
        getlist = this.data.need_list[i]
      }
    }
    console.log(getlist,"ididididididid")
    let tmplist = {
      current_task_id:getlist.id,
      report_num:this.data.setworknum,
      order_id:this.data.order_id,
      stage:getlist.stage
    }

    console.log("tmplist",tmplist)

    request.requestAjax('order/report_production', tmplist, "提交中", function(res) {
      if (res.status == 200) {
        wx.showToast({
          title: '成功提交',
          icon: "success"
      })
      let tmp = {
        id:that.data.p_id
      }
      that.setDataShow(tmp)
      } else {
          wx.showToast({
              title: '请求失败',
              icon: "none"
          })
      }

  }, function(res) {
      wx.showToast({
          title: '请求失败',
          icon: "none"
      })
    });
  },
  nextworl(e){
    var that = this
    let tmplist = {
      order_id:that.data.need_list[that.data.radioValue].order_id,
      current_task_id:that.data.need_list[that.data.radioValue].id,
      stage:that.data.no
    }
    let tmpId = {
      id:that.data.options.tmp
    }
    request.requestAjax('order/skip_task', tmplist, "提交中", function(res) {
      if (res.status == 200) {
          that.getData(tmpId,that.data.options)
      } else {
          wx.showToast({
              title: '请求失败',
              icon: "none"
          })
      }

     }, function(res) {
      wx.showToast({
          title: '请求失败',
          icon: "none"
      })
    });
  },
  setworknum(e){
    this.setData({
      setworknum:e.detail
    })
  },
  production_record(){
    wx.navigateTo({  url: `/pages/production_record/production_record?id=${this.data.order_id}` })
  }
  
})